// 处理数据
const strPramas = (pramas) => {
  let query = []
  for (let k in pramas) {
    const str = k + `=` + pramas[value]
    query.push(str)
  }
  return query.join('&')
}
// 封装promise实例
function myAjax(option) {
  return new Promise((res, rej) => {
    const xhr = new XMLHttpRequest()
    const query = strPramas(option.data)
     // 判断请求方式---GET
    if (option.method.toUpperCase() === 'GET') {
      xhr.open(option.method, option.url)
      xhr.send()
      // 判断请求方式---POST
    } else if (option.method.toUpperCase === 'POST') {
      xhr.open(option.method, option.url)
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      xhr.send(query)
    }
    // 监听事件
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          res(JSON.parse(xhr.responseText))
        } else {
          rej(xhr.status)
        }
      }
    }
  })
}